package com.example.attempt.controller;

import com.example.attempt.entity.Comment;
import com.example.attempt.entity.Post;
import com.example.attempt.service.impl.*;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
public class PostController {
    @Autowired
    private PostServiceImpl postImpl;
    @Autowired
    private StudentServiceImpl studentImpl;
    @Autowired
    private TeacherServiceImpl teacherImpl;
    @Autowired
    private AssistantServiceImpl assistantImpl;
    @Autowired
    private CourseServiceImpl courseImpl;
    @Autowired
    private CommentServiceImpl commentImpl;

    @PostMapping(path = "/queryAllPost")
    public List<Post> queryAllPost(){
        return postImpl.queryAll();
    }
    @PostMapping(path = "/queryPostBySid")
    public List<Post> queryPostBySid(@RequestParam(value = "sid") String sid){
        if (studentImpl.queryById(sid) == null){
            return null; //该学生不存在
        }
        return postImpl.queryBySid(sid);
    }
    @PostMapping(path = "/checkContent")
    public int checkContent(String content) throws IOException {
        System.out.println(content);
        OkHttpClient client=new OkHttpClient();
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("content", content);
        String url="https://aigc.tjsj.xyz:8081/image/check_content";
        okhttp3.RequestBody requestBody = okhttp3.RequestBody.create(
                MediaType.parse("application/json"),
                jsonBody.toString()
        );
        // 构建请求
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();

        // 发送请求并获取响应
        Response response = client.newCall(request).execute();
        String json = response.body().string();
        System.out.println(json);
        JSONObject jsonObject = new JSONObject(json);
        int code=(int)jsonObject.get("code");
        return code;
    }
    @PostMapping(path = "/queryPostByTid")
    public List<Post> queryPostByTid(@RequestParam(value = "tid") String tid){
        if (teacherImpl.queryById(tid) == null){
            return null; //该老师不存在
        }
        return postImpl.queryByTid(tid);
    }

    @PostMapping(path = "/queryPostByAid")
    public List<Post> queryPostByAid(@RequestParam(value = "aid") String aid){
        if (assistantImpl.queryById(aid) == null){
            return null; //该助教不存在
        }
        return postImpl.queryByAid(aid);
    }

    @PostMapping(path = "/queryPostByCid")
    public List<Post> queryPostByCid(@RequestParam(value = "cid") String cid){
        if (courseImpl.queryById(cid) == null){
            return null; //该课程不存在
        }
        return postImpl.queryByCid(cid);
    }

    @PostMapping(path = "/queryPostByPid")
    public Post queryPostByPid(@RequestParam(value = "pid") int pid){
        return postImpl.queryByPid(pid);
    }

    @PostMapping(path = "/addPost")
    public int addPost(@RequestBody Post post){
        System.out.println(post);
        if (postImpl.queryByPid(post.getPid()) != null){
            return 1; //已有该帖子
        }
        if (studentImpl.queryById(post.getSid())==null &&
                teacherImpl.queryById(post.getTid())==null &&
                assistantImpl.queryById(post.getAid())==null ){
            return 2; //不存在发帖人
        }
        if (post.getPcontent()==null || post.getPtitle()==null){
            return 3; //帖子没有题目或内容
        }
        if(courseImpl.queryById(post.getCid())==null || post.getCid()==null){
            return 4; //课程号不存在或课程号为空
        }
        System.out.println("进来添加帖子了捏");

        post.setPtime();
        return postImpl.addPost(post);
    }

    @PostMapping(path = "/deletePost")
    public int deletePost(@RequestParam(value = "pid") int pid){
        if (postImpl.queryByPid(pid) == null){
            return 1; //帖子不存在
        }
        //删除帖子的所有评论
        List<Comment> comments = commentImpl.queryByPid(pid);
        for (Comment c : comments) {
            commentImpl.deleteComment(c.getCommentId());
        }
        return postImpl.deletePost(pid);
    }

    @PostMapping(path = "/queryAllComment")
    public List<Comment> queryAllComment(){
        return commentImpl.queryAll();
    }
    @PostMapping(path = "/queryCommentBySid")
    public List<Comment> queryCommentBySid(@RequestParam(value = "sid") String sid){
        if (studentImpl.queryById(sid) == null){
            return null; //该学生不存在
        }
        return commentImpl.queryBySid(sid);
    }
    @PostMapping(path = "/queryCommentByTid")
    public List<Comment> queryCommentByTid(@RequestParam(value = "tid") String tid){
        if (teacherImpl.queryById(tid) == null){
            return null; //该老师不存在
        }
        return commentImpl.queryByTid(tid);
    }

    @PostMapping(path = "/queryCommentByAid")
    public List<Comment> queryCommentByAid(@RequestParam(value = "aid") String aid){
        if (assistantImpl.queryById(aid) == null){
            return null; //该助教不存在
        }
        return commentImpl.queryByAid(aid);
    }

    @PostMapping(path = "/queryCommentByCommentId")
    public Comment queryCommentByCommentId(@RequestParam(value = "commentId") int commentId){
        if (commentImpl.queryByCommentId(commentId) == null){
            return null; //该课程不存在
        }
        return commentImpl.queryByCommentId(commentId);
    }

    @PostMapping(path = "/queryCommentByPid")
    public List<Comment> queryCommentByPid(@RequestParam(value = "pid") int pid){
        if (postImpl.queryByPid(pid) == null){
            return null; //该帖子不存在
        }
        return commentImpl.queryByPid(pid);
    }

    @PostMapping(path = "/addComment")
    public int addComment(@RequestBody Comment comment){
        if (commentImpl.queryByCommentId(comment.getCommentId()) != null){
            return 1; //已有该评论
        }
        if (studentImpl.queryById(comment.getSid())==null &&
                teacherImpl.queryById(comment.getTid())==null &&
                assistantImpl.queryById(comment.getAid())==null ){
            return 2; //不存在评论人
        }
        if (comment.getCommentContent()==null){
            return 3; //评论没有内容
        }
        if(postImpl.queryByPid(comment.getPid())==null){
            return 4; //帖子号不存在
        }
        System.out.println("进来添加帖子了捏");

        comment.setcommentTime();
        return commentImpl.addComment(comment);
    }

    @PostMapping(path = "/deleteComment")
    public int deleteComment(@RequestParam(value = "commentId") int commentId){
        if (commentImpl.queryByCommentId(commentId) == null){
            return 1; //评论不存在
        }
        return commentImpl.deleteComment(commentId);
    }

    @PostMapping(path = "/addReply")
    public int addReply(@RequestBody Comment comment){
        if (commentImpl.queryByCommentId(comment.getReply()) == null){
            return 1; // 回复的评论不存在
        }
        else{
            if(addComment(comment)==0)
                return 0;
        }
        return -1; //回复失败
    }
}
